let app = getApp()
import {
  request,
  requestHasToken
} from '../../service/network.js'
import {
  previewImg,
  previewImgList
} from '../../behavior/previewImg/previewImg.js'
Page({
  behaviors: [previewImg, previewImgList], //  继承behavior.js里面的properties，data，methods
  /**
   * 页面的初始数据
   */
  data: {
    baseurl: App.config.baseApi,
    isSubmit: false,
    warn: "",
    pic: '',
    genderIndex: 0,
    genders: ['男', '女'],
    marriagesIndex: 0,
    marriages: ['未婚', '离异'],
    typeIndex: 0,
    types: ['公开', '隐藏'],
    addressIndex: 0,
    addresses: ['鄞州区', '海曙区', '江北区', '江东区', '北仑区', '镇海区', '奉化区', '象山', '慈溪', '余姚', '宁海', '台州', '其他'],
    admin: false,
    normalAdmin: false,
    info: {
      id: '',
      userAvatar: '',
      userNickName: '',
      openid: '',
      userId: '',
      nickname: '',
      gender: '1',
      age: '',
      phone: '',
      marriage: '未婚',
      education: '',
      occupation: '',
      car: '',
      house: '',
      pic: '',
      height: '',
      weight: '',
      comment: '',
      nativeplace: '',
      address: '鄞州区',
      hobby: '',
      requirement: '',
      reward: '0',
      type: '公开',
      state: ''
    }
  },
  onLoad: function (options) {
    // console.log(options);

    if (app.globalData.admin) {
      this.setData({
        admin: true,
      })
    }
    if (app.globalData.normalAdmin) {
      this.setData({
        normalAdmin: true,
      })
    }
    let url = 'api/ap/information/getSelf'
    let data = {}
    if (this.data.normalAdmin || this.data.admin) {
      url = 'api/ap/information/get'
      if (options.id) {
        data.id = options.id
      }
    }
    requestHasToken({
      url,
      data,
    }).then((res) => {
      // console.log(res);
      let infoData
      if ((this.data.normalAdmin || this.data.admin) && options.id) {
        if (res.data.code == 0 && res.data.data) {
          infoData = res.data.data
        }
      } else {
        if (res.data.code == 0 && res.data.data.length > 0) {
          infoData = res.data.data[0]
        }
      }
      if (res.data.code == 0 && infoData) {
        this.setData({
          'info.id': infoData.id,
          'info.userAvatar': infoData.userAvatar,
          'info.userNickName': infoData.userNickName,
          'info.openid': infoData.openid,
          'info.userId': infoData.userId,
          'info.nickname': infoData.nickname,
          'info.gender': infoData.gender,
          'info.age': infoData.age,
          'info.phone': infoData.phone,
          'info.marriage': infoData.marriage,
          'info.education': infoData.education,
          'info.occupation': infoData.occupation,
          'info.car': infoData.car,
          'info.house': infoData.house,
          'info.pic': infoData.pic,
          'info.height': infoData.height,
          'info.weight': infoData.weight,
          'info.comment': infoData.comment,
          'info.nativeplace': infoData.nativeplace,
          'info.address': infoData.address,
          'info.hobby': infoData.hobby,
          'info.requirement': infoData.requirement,
          'info.type': infoData.type,
          'info.reward': infoData.reward,
          'info.state': infoData.state
        })
      }
    })
  },
  onShareAppMessage: function () {

  },
  handleGender(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      genderIndex: e.detail.value,
      'info.gender': parseInt(e.detail.value) + 1
    })
  },
  bindDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      ageIndex: e.detail.value,
      'info.age': e.detail.value
    })
  },
  handleMarriage: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      marriagesIndex: e.detail.value,
      'info.marriage': this.data.marriages[e.detail.value]
    })
  },
  handleType: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      typeIndex: e.detail.value,
      'info.type': this.data.types[e.detail.value]
    })
  },
  handleAddresses: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      addressIndex: e.detail.value,
      'info.address': this.data.addresses[e.detail.value]
    })
  },
  handlePic(res) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        this.setData({
          'pic': tempFilePaths[0],
        })
        wx.uploadFile({
          url: App.config.baseApi + 'api/xcxFile/upload',
          header: {
            Authorization: 'Bearer ' + wx.getStorageSync('token'),
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8' // 默认值
          },
          filePath: this.data.pic,
          name: 'file',
          formData: {},
          success: (res2) => {
            console.log(res2);

            let infoic = JSON.parse(res2.data).url
            this.setData({
              'info.pic': infoic,
            })
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    let _this = this
    let {
      nickname,
      phone,
      education,
      occupation,
      car,
      house,
      height,
      weight,
      comment,
      nativeplace,
      hobby,
      reward,
      requirement,
    } = e.detail.value;

    if (!nickname || !this.data.info.age || !height || !weight || !nativeplace || !requirement ) {
      this.setData({
        warn: "请填写红色星号的必填项！",
        isSubmit: true
      })
    } else {
      this.setData({
        warn: "",
        isSubmit: false,
        'info.nickname': nickname,
        'info.phone': phone,
        'info.education': education,
        'info.occupation': occupation,
        'info.car': car,
        'info.house': house,
        'info.height': height,
        'info.weight': weight,
        'info.comment': comment,
        'info.nativeplace': nativeplace,
        'info.hobby': hobby,
        'info.reward': reward,
        'info.requirement': requirement,
        'info.state': '未审核',
      })
      if(this.data.admin || this.data.normalAdmin) {
        this.setData({
          'info.state': '已审核'
        })
      }
      if (this.data.info.id) {
        requestHasToken({
          url: 'api/ap/information/update',
          data: this.data.info,
        }).then((res) => {
          if (res.data.code == 0) {
            wx.redirectTo({
              url: '/pages/infomation/infomation'
            })
          }
        })
      } else {
        requestHasToken({
          url: 'api/ap/information/save',
          data: this.data.info,
        }).then((res) => {
          if (res.data.code == 0) {
            wx.redirectTo({
              url: '/pages/infomation/infomation'
            })
          }
        })
      }
    }
  },
  handleTotal(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({
      'info.reward': value
    })
  },
  validateNumber(val) {
    return val.replace(/[^0-9\.-]/g, '')
  },
  validateBlueTotal(e) {
    let value = e.detail.value
    if (parseFloat(value) != NaN) {
      value = parseFloat(e.detail.value)
    } else {
      value = 0
    }
    this.setData({
      'info.reward': value
    })
  },
})